/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.component.command;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.ui.PlatformUI;
import org.eclipse.uml2.uml.CollaborationUse;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Property;

import com.cea.papyrus.core.commands.connection.ConnectionCreateCommand;
import com.cea.papyrus.core.ui.dialogs.ChooseRoleDialog;
import com.cea.papyrus.diagram.classdiagram.helper.EdgeDependencyHelper;
import com.cea.papyrus.diagram.component.helper.EdgeRoleBindingHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * 
 * 
 * @author CEA List
 * TODO: comment this class
 */
public class RoleBindingCreateCommand extends ConnectionCreateCommand {
	
	/**
	 * Instantiate a command that can create a connection between two Elements.
	 */
	CollaborationUse cu= null;
	
	/**
	 * 
	 */
	Property sourceProperty;
	
	/**
	 * 
	 */
	Property targetProperty;
	
	/**
	 * 
	 */
	Dependency dependency;
	
	/**
	 * 
	 * 
	 * @param type 
	 * @param location 
	 * @param source 
	 */
	public RoleBindingCreateCommand(GraphElement source, Object type, Point location) {
		super(source, type, location);
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public Element createBussinessModel() {
		if (getSourceElement() instanceof CollaborationUse){
			cu=(CollaborationUse)getSourceElement();
			sourceProperty=(Property)getTargetElement();
		}
		else{
			cu=(CollaborationUse)getTargetElement();
			sourceProperty=(Property)getSourceElement();
		}
		ChooseRoleDialog crd= new ChooseRoleDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),cu);
		crd.open();
		targetProperty=(Property)crd.getValue();
		if (targetProperty!=null){
			EdgeDependencyHelper edh= new EdgeDependencyHelper();
			dependency=(Dependency)edh.createUMLElement(cu);
			edh.connect(sourceProperty, targetProperty, dependency);
		}
		return cu; 
	}
	
	/**
	 * 
	 * 
	 * @param bridge 
	 * @param targetBridge 
	 * @param sourceBridge 
	 */
	public void reconnectBussinessModel(Uml1SemanticModelBridge bridge, Uml1SemanticModelBridge sourceBridge, Uml1SemanticModelBridge targetBridge) {

		
		ChooseRoleDialog crd= new ChooseRoleDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),cu);
		crd.open();
		targetProperty=(Property)crd.getValue();
		if (targetProperty!=null){
			EdgeDependencyHelper edh= new EdgeDependencyHelper();
			dependency=(Dependency)edh.createUMLElement(cu);
			edh.connect(sourceProperty, targetProperty, dependency);
		}
	}

	/**
	 * 
	 * 
	 * @param bridge 
	 * @param targetBridge 
	 * @param sourceBridge 
	 */
	public void disconnectBussinessModel(Uml1SemanticModelBridge bridge, Uml1SemanticModelBridge sourceBridge, Uml1SemanticModelBridge targetBridge) {
		EdgeDependencyHelper edh= new EdgeDependencyHelper();
		edh.removeUMLElement(dependency, cu);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.connections.commands.ConnectionCreateCommandBase#createGraphEdge(org.eclipse.uml2.uml.Element)
	 */
	//@Override
	/**
	 * 
	 * 
	 * @param umlLink 
	 * 
	 * @return 
	 */
	protected GraphEdge createGraphEdge(Element umlLink) {
		EdgeRoleBindingHelper erbh= new EdgeRoleBindingHelper();
		return  (GraphEdge)erbh.createGraphElement(cu, getOwner(), null, null);
	}



}
